package com.buba.controller.admin;

import com.alibaba.fastjson.JSON;
import com.buba.pojo.Floor;
import com.buba.service.FloorService;
import com.buba.service.RoomService;
import com.buba.utils.DataGridViewResult;
import com.buba.utils.SystemConstant;
import com.buba.vo.FloorVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/floor")
public class FloorController {
    @Resource
    private FloorService floorService;
    @Resource
    private RoomService roomService;

    /**
     * todo 查询所有楼层
     * @param floorVO
     * @return
     */
    @RequestMapping("/List")
    public DataGridViewResult List(FloorVO floorVO) {
        //设置分页信息
        PageHelper.startPage(floorVO.getPage(), floorVO.getLimit());
        // 调用查询楼层列表的方法
        List<Floor> floorList = floorService.findFloorList(floorVO);
        // 创建分页对象
        PageInfo<Floor> pageInfo = new PageInfo<Floor>(floorList);
        // 返回数据
        return new DataGridViewResult(pageInfo.getTotal(), pageInfo.getList());


    }

    /**
     *todo  添加楼层
     *
     * @param floor
     * @return
     */
    @RequestMapping("/addFloor")
    public String addFloor(Floor floor) {
        HashMap<String, Object> map = new HashMap<String, Object>();
//        todo 调用添加部门的方法
        if (floorService.addFloor(floor) > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "添加成功");

        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * todo 修改楼层
     *
     * @param floor
     * @return
     */
    @RequestMapping("/updateFloor")
    public String updateFloor(Floor floor) {
        HashMap<String, Object> map = new HashMap<String, Object>();
//        todo 调用修改部门的方法
        if (floorService.updateFloor(floor) > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "修改成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * todo 删除楼层
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public String deleteById(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用删除楼层的方法
        if (floorService.deleteById(id) > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "删除成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * todo 检查该楼层下是否存在房间信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/checkFloorHasRoom")
    public String checkFloorHasRoom(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        //todo 调用根据楼层编号查询房间数量的方法
        if (roomService.getRoomCountByFloorId(id) > 0) {
            map.put(SystemConstant.EXIST, true);
            //存在
            map.put(SystemConstant.MESSAGE, "该部门存在房间信息，无法删除");
        } else {
            map.put(SystemConstant.EXIST, false);//不存在
        }
        return JSON.toJSONString(map);
    }


}
